package com.zhy.sunmi.fragment.sell;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.zhy.sunmi.R;
import com.zhy.sunmi.bean.SellGoodHistory;
import com.zhy.sunmi.bean.SellingGood;
import com.zhy.sunmi.bean.SoldGoodDetail;

import java.util.ArrayList;
import java.util.List;


/**
 * @author zhouhongyang
 */
public class SubSellingFragment extends Fragment {
    private ViewPager viewPager;
    private List<Fragment> fragments = new ArrayList<>();
    private List<SellingGood> sellgoods = new ArrayList<>();
    private SubPayFragment subPayFragment;
    private SubSellFragment subSellFragment;
    private SubRefundFragment subRefundFragment;
    private SubRefundPayFragment subRefundPayFragment;
    private View rootView;

    public SubSellingFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment


        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_sub_selling, container, false);
            initView(rootView);
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }


        return rootView;


    }

    private void initView(View view) {
        viewPager = view.findViewById(R.id.viewpager);

        fragments.add(subSellFragment = new SubSellFragment());
        fragments.add(subPayFragment = new SubPayFragment());
        fragments.add(subRefundFragment = new SubRefundFragment());
        fragments.add(subRefundPayFragment = new SubRefundPayFragment());


        viewPager.setAdapter(new SectionsPagerAdapter(this.getChildFragmentManager(), fragments));
        viewPager.setCurrentItem(0);

    }


    private class SectionsPagerAdapter extends FragmentPagerAdapter {
        List<Fragment> fragments;


        public SectionsPagerAdapter(@NonNull FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    public void goShopping(int i, SellGoodHistory sellGoodHistory, String a) {
        viewPager.setCurrentItem(i);

        if (i == 0) {
            if (a != null) {
                subSellFragment.go(sellGoodHistory);
            }

        }

        if (i == 1) {
            subPayFragment.go(sellGoodHistory);
        }

        if (i == 2) {
            subRefundFragment.go(sellGoodHistory);
        }

    }


    public void goRefundPay(List<SoldGoodDetail> soldGoodDetails,String notes) {
        viewPager.setCurrentItem(3);


        subRefundPayFragment.go(soldGoodDetails,notes);


    }

}